package com.freak.neteasecloudmusic.modules.base;

import com.freak.httphelper.ApiCallback;
import com.freak.httphelper.HttpMethods;
import com.freak.httphelper.RxPresenter;
import com.freak.httphelper.SubscriberCallBack;
import com.freak.neteasecloudmusic.app.api.ApiService;
import com.freak.neteasecloudmusic.commom.constants.Constants;
import com.freak.neteasecloudmusic.modules.base.entity.LoginStatusEntity;
import com.freak.neteasecloudmusic.net.log.LogUtil;
import com.google.gson.Gson;

import io.reactivex.Observable;

/**
 * @author Administrator
 * @date 2019/3/16
 */

public class MainPresenter extends RxPresenter<MainContract.View> implements MainContract.Presenter {
    ApiService mApiService = HttpMethods.getInstance().create(ApiService.class);

    @Override
    public void loadLoginStatusEntity() {
        Observable<LoginStatusEntity> observable = mApiService.loadLoginStatus();
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<LoginStatusEntity>() {
            @Override
            public void onSuccess(LoginStatusEntity model) {
                if (model.getCode() == Constants.SUCCESS_CODE) {
                    mView.getLoginStatusSuccess(model);
                } else {

                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.e(msg);
                mView.getLoginStatusError();
            }
        }));
    }
    @Override
    public void loginOut() {
        Observable<Object> observable = mApiService.loginOut();
        addSubscription(observable, new SubscriberCallBack<>(new ApiCallback<Object>() {
            @Override
            public void onSuccess(Object model) {
                LogUtil.e(new Gson().toJson(model));
                mView.loginOutSuccess();
//                if (model.getCode() == Constants.SUCCESS_CODE) {
//                    if (mView==null){
//                        LogUtil.e("mView 为空");
//                    }else {
//                        mView.getVideoSuccess(model);
//                    }
//                } else {
//                    ToastUtil.shortShow("获取数据失败");
//                }
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
            }
        }));
    }

}
